package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.FaceCheckAuthRequest;
import com.ssy.lingxi.contract.esign.request.FaceRealNameAuthRequest;
import com.ssy.lingxi.contract.esign.request.TelecomCheckAuthRequest;
import com.ssy.lingxi.contract.esign.request.TelecomRealNameAuthRequest;
import com.ssy.lingxi.contract.esign.response.FaceAuthResponse;
import com.ssy.lingxi.contract.esign.response.TelecomAuthResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人认证API
 * @author yzc
 * @version 2.0.0
 * @date 2020/9/8
 */
public class PersonalAuth {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public PersonalAuth(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }

    /**
     * 发起个人刷脸实名认证
     */
    public FaceAuthResponse faceRealNameAuth(FaceRealNameAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/individual/{accountId}/face";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, FaceAuthResponse.class);
    }

    /**
     * 发起个人刷脸核身认证
     */
    public FaceAuthResponse faceCheckAuth(FaceCheckAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/individual/face";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, FaceAuthResponse.class);
    }

    /**
     * 发起运营商3要素实名认证
     */
    public TelecomAuthResponse telecomRealNameAuth(TelecomRealNameAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/individual/{accountId}/telecom3Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, TelecomAuthResponse.class);
    }

    /**
     * 发起运营商3要素核身认证
     */
    public TelecomAuthResponse telecomCheckAuth(TelecomCheckAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/individual/telecom3Factors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, TelecomAuthResponse.class);
    }

    /**
     * 运营商短信验证码校验
     */
    public void telecomAuthCode(String flowId, String authcode) throws EsignException {
        String resourcePath = "/v2/identity/auth/pub/individual/{flowId}/telecom3Factors";
        RequestMethod method = RequestMethod.PUT;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("flowId", flowId);
        paramMap.put("authcode", authcode);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }
}